package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.PayRoll;
import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.mapper.PayRollMapper;
import cn.wolfcode.crm.query.EmployeeQueryObject;
import cn.wolfcode.crm.query.PayRollQueryObject;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IPayRollService;
import cn.wolfcode.crm.util.PageResult;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018/3/24.
 */
@Service
public class PayRollServiceImpl implements IPayRollService {
    @Autowired
    private PayRollMapper mapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return mapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(PayRoll record) {
        return mapper.insert(record);
    }

    @Override
    public PayRoll selectByPrimaryKey(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public List<PayRoll> selectAll() {
        return mapper.selectAll();
    }

    @Override
    public int updateByPrimaryKey(PayRoll record) {
        return mapper.updateByPrimaryKey(record);
    }

    @Override
    public PageResult query(PayRollQueryObject qo) {
        int total = mapper.queryForCount(qo);
        if (total == 0) {
            return new PageResult(0, Collections.EMPTY_LIST);
        }
        return new PageResult(total, mapper.queryForList(qo));
    }

    //核算
    @Override
    public void onState(Long id) {
        //通过id查询出该条数据
        PayRoll payRoll = mapper.selectByPrimaryKey(id);
        Integer workDay = payRoll.getWorkDay();
        Integer afterNumber = payRoll.getAfterNumber();
        Integer beforeNumber = payRoll.getBeforeNumber();
        if (payRoll.getState()) {
            throw new RuntimeException("已清结工资不得再次结算!");
        } else {
            if (payRoll.getNowSalary() != null && payRoll.getNowSalary().compareTo(BigDecimal.ZERO) != 0) {
                payRoll.setPayTime(new Date());
                payRoll.setState(true);
                //执行结算工资的方法
                mapper.updateById(payRoll);
            } else {//当最终工资为null或为0时
                if (workDay >= 20 && (afterNumber + beforeNumber) < 15) {
                    throw new RuntimeException("你工作了:" + workDay + "天,迟到了:" + afterNumber + "天,早退了:"
                            + beforeNumber + "天     " + "若是觉得有问题请联系财务");
                } else {
                    throw new RuntimeException("你工作了:" + workDay + "天,迟到了:" + afterNumber
                            + "天,早退了:" + beforeNumber + "天!你还想要工资");
                }
            }
        }
    }

    @Override
    public void importXls(MultipartFile file, HttpServletResponse response) throws Exception {
        if (file != null) {
            InputStream inputStream = null;
            inputStream = file.getInputStream();
            Workbook wb = new HSSFWorkbook(inputStream);
            Sheet sheet = wb.getSheetAt(0);
            //创建Excel文件,指定输入流
            //获取最后一行的索引
            int lastRowNum = sheet.getLastRowNum();
            for (int i = 1; i <= lastRowNum; i++) {
                PayRoll payRoll = new PayRoll();
                Row row = sheet.getRow(i);
                payRoll.setEmployeeName(row.getCell(0).getStringCellValue());
                payRoll.setMonth(row.getCell(1).getDateCellValue());
                payRoll.setSalary(BigDecimal.valueOf(row.getCell(2).getNumericCellValue()));
                payRoll.setWorkDay((int) row.getCell(3).getNumericCellValue());
                payRoll.setAfterNumber((int) row.getCell(4).getNumericCellValue());
                payRoll.setBeforeNumber((int) row.getCell(5).getNumericCellValue());
                payRoll.setUpNumber((int) row.getCell(6).getNumericCellValue());
                payRoll.setNowSalary(BigDecimal.valueOf(row.getCell(7).getNumericCellValue()));
                payRoll.setPayTime(row.getCell(8).getDateCellValue());
                payRoll.setState(true);
                mapper.insert(payRoll);
            }
        } else {
            throw new RuntimeException("你都没有选择文件,还想上传个毛线啊~");
        }
    }

    /**
     * 根据条件查询员工列表导出到exl
     *
     * @param qo
     * @return
     */
    @Override
    public Workbook exportXls(PayRollQueryObject qo) {
        //创建xls
        Workbook book = new HSSFWorkbook();
        //创建工作簿
        Sheet sheet = book.createSheet();
        //设置第一行的标题
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("员工姓名");
        row.createCell(1).setCellValue("月份");
        row.createCell(2).setCellValue("基础工资");
        row.createCell(3).setCellValue("工作天数");
        row.createCell(4).setCellValue("迟到天数");
        row.createCell(5).setCellValue("早退天数");
        row.createCell(6).setCellValue("补签天数");
        row.createCell(7).setCellValue("最终工资");
        row.createCell(8).setCellValue("结算时间");
        row.createCell(9).setCellValue("是否结算");
        List<PayRoll> list = mapper.queryForList(qo);
        for (int i = 0; i < list.size(); i++) {
            try {
                PayRoll payRoll = (PayRoll) list.get(i);
                //创建行
                row = sheet.createRow(i + 1);
                row.createCell(0).setCellValue(payRoll.getEmployeeName());
                row.createCell(1).setCellValue(payRoll.getMonth());
                row.createCell(2).setCellValue(payRoll.getSalary().longValue());
                row.createCell(3).setCellValue(payRoll.getWorkDay());
                row.createCell(4).setCellValue(payRoll.getAfterNumber());
                row.createCell(5).setCellValue(payRoll.getBeforeNumber());
                row.createCell(6).setCellValue(payRoll.getUpNumber());
                row.createCell(7).setCellValue(payRoll.getNowSalary().longValue());
                row.createCell(8).setCellValue(payRoll.getPayTime());
                row.createCell(9).setCellValue(payRoll.getState());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return book;
    }

    /**
     * 用户获取获取模板
     *
     * @return
     */
    public Workbook getImportXlsMode() {
        //创建xls
        Workbook book = new HSSFWorkbook();
        //创建工作簿
        Sheet sheet = book.createSheet();
        //设置第一行的标题
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("员工名称");
        row.createCell(1).setCellValue("月份");
        row.createCell(2).setCellValue("基础工资");
        row.createCell(3).setCellValue("工作天数");
        row.createCell(4).setCellValue("迟到天数");
        row.createCell(5).setCellValue("早退天数");
        row.createCell(6).setCellValue("补签天数");
        row.createCell(7).setCellValue("最终工资");
        row.createCell(8).setCellValue("结算时间");
        row.createCell(9).setCellValue("是否结算");
        return book;
    }

}
